﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ICement_Buying.Code;
using OA.Model;
using OA.Business;
using System.Collections;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace ICement_Buying
{
    public partial class PostData : PageBase
    {
        public IProductBusiness ProBll { get; set; }
        Product product = new Product();
        Hashtable hashtable = new Hashtable(); 
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            CheckData4();
            return;
            int id = QueryParamInt32("id", 0);
            if (id>0 && IsPost)
            {
                product.ProId = FormParamInt32("proId", 0);
                if (product.ProId > 0)
                {
                    product = ProBll.GetById(product.ProId);
                    if (product == null)
                    {
                        product = new Product();
                        product.ProId = FormParamInt32("proId", 0);
                        if (id == 1)
                        {
                            CheckData();
                        }
                        else if (id == 2)
                        {
                            CheckData2();
                        }
                        else if (id == 3)
                        {
                            CheckData3();
                        }
                        ProBll.Insert(product);
                    }
                    else
                    {
                        if (id == 1)
                        {
                            CheckData();
                        }
                        else if (id == 2)
                        {
                            CheckData2();
                        }
                        else if (id == 3)
                        {
                            CheckData3();
                        }
                        hashtable.Add(Em_Product.ProId, product.ProId);
                        ProBll.UpDate(hashtable);
                    }
                }
                else
                {
                    if (id == 4)
                    {
                        CheckData4();
                    }
                }
            }
        }
        private void CheckData2()
        {
            product.main_param = FormParam("main_param", "");
            product.detail_param = FormParam("detail_param", "");
            hashtable.Clear();
            hashtable.Add(Em_Product.main_param, product.main_param);
            hashtable.Add(Em_Product.detail_param, product.detail_param);
            product.SerializeXmlFile("d:\\11.xml");
        }
        private void CheckData3()
        {
            product.pro_Pic = FormParam("pro_Pic", "");
            string pics = FormParam("pics", "");//格式
            //<img .src="http://2b.zol-img.com.cn/product/17_160x120/215/cegNBlva4IWg.jpg" alt="" />                实拍图-1                                                                      <img .src="http://2f.zol-img.com.cn/product/15_160x120/799/ceH7qVdkY08qI.jpg" alt="" />                实拍图-2                                                                      <img .src="http://2a.zol-img.com.cn/product/17_160x120/214/ce18aTwgMjPg.jpg" alt="" />                实拍图-3                                                                      <img .src="http://2f.zol-img.com.cn/product/15_160x120/787/ceNOhKimI2sgo.jpg" alt="" />                实拍图-4                                                                      <img .src="http://2d.zol-img.com.cn/product/15_160x120/785/ce9TtSEwSdBg2.jpg" alt="" />                实拍图-5                                                                      <img .src="http://2e.zol-img.com.cn/product/13_160x120/332/ceDCqUgbfOO6.jpg" alt="" />                实拍图-6                                                                      <img .src="http://2c.zol-img.com.cn/product/12_160x120/316/ceYg9FZYWQMM6.jpg" alt="" />                整体外观-1                                                                      <img .src="http://2a.zol-img.com.cn/product/15_160x120/788/ceN8RgAXFkjOU.jpg" alt="" />                实拍图-7                                                                      <img .src="http://2d.zol-img.com.cn/product/12_160x120/317/ceaj9II0G0BVE.jpg" alt="" />                整体外观-2                                                                      <img .src="http://2f.zol-img.com.cn/product/15_160x120/793/ceNbrPK8Xbq2.jpg" alt="" />                实拍图-8                                                                      <img .src="http://2f.zol-img.com.cn/product/7_160x120/727/cesEA2dER5E2.jpg" alt="" />                整体外观-3                                                                      <img .src="http://2b.zol-img.com.cn/product/15_160x120/795/ce1D6mswe0Zj.jpg" alt="" />                实拍图-9                                                                      <img .src="http://2d.zol-img.com.cn/product/7_160x120/731/cemEljFlkAgYw.jpg" alt="" />                整体外观-4                                                                      <img .src="http://2d.zol-img.com.cn/product/15_160x120/791/ceDwtwqtoZ77Q.jpg" alt="" />                实拍图-10                                                                      <img .src="http://2b.zol-img.com.cn/product/15_160x120/789/ceKjfEykpdfFA.jpg" alt="" />                实拍图-11                                                                      <img .src="http://2d.zol-img.com.cn/product/17_160x120/211/ceXqBHSfMz6wY.jpg" alt="" />                机身细节-1                                                                      <img .src="http://2a.zol-img.com.cn/product/17_160x120/208/ceB8ncUk8yYrI.jpg" alt="" />                机身细节-2                                                                      <img .src="http://2d.zol-img.com.cn/product/17_160x120/205/ceCmV3I7JH3c.jpg" alt="" />                整体外观-5                                                                      <img .src="http://2b.zol-img.com.cn/product/17_160x120/203/ce6V4dacpZ5s.jpg" alt="" />                机身细节-3                                                                      <img .src="http://2e.zol-img.com.cn/product/17_160x120/200/ceblQCaWkMqMA.jpg" alt="" />                机身细节-4                                                                      <img .src="http://2b.zol-img.com.cn/product/17_160x120/209/cewx1qliqUl8w.jpg" alt="" />                机身细节-5                                                                      <img .src="http://2f.zol-img.com.cn/product/17_160x120/213/cehRkWRApJ2Uc.jpg" alt="" />                实拍图-12                                                                      <img .src="http://2d.zol-img.com.cn/product/15_160x120/797/cewt2FqInYmNo.jpg" alt="" />                实拍图-13                                                                      <img .src="http://2e.zol-img.com.cn/product/17_160x120/212/celNji24NlAfs.jpg" alt="" />                机身细节-6                                                                      <img .src="http://2c.zol-img.com.cn/product/17_160x120/210/cegPLmd3inP.jpg" alt="" />                机身细节-7                                                                      <img .src="http://2c.zol-img.com.cn/product/7_160x120/730/ceBnmMUoLbM.jpg" alt="" />                整体外观-6
            hashtable.Clear();
            hashtable.Add(Em_Product.pro_Pic, product.pro_Pic);
        }
        private void CheckData()
        {
            product.ProName = FormParam("name", "");
            product.subcateId = FormParamInt32("subcateId", 0);
            product.manuId = FormParamInt32("manuId", 0);
            product.proCate = FormParam("type", "");
            product.brand = FormParam("brand", "");
            product.price_Indication = FormParam("price_Indication", "");
            ToPrice(product.price_Indication);
            product.quoted_Price = FormParam("quoted_Price", "");
            ToQuoPrice(product.quoted_Price);
            hashtable.Clear();
            hashtable.Add(Em_Product.ProName, product.ProName);
            hashtable.Add(Em_Product.subcateId, product.subcateId);
            hashtable.Add(Em_Product.manuId, product.manuId);
            hashtable.Add(Em_Product.proCate, product.proCate);
            hashtable.Add(Em_Product.brand, product.brand);
            hashtable.Add(Em_Product.price_Indication, product.price_Indication);
            hashtable.Add(Em_Product.pro_Price, product.pro_Price);
            hashtable.Add(Em_Product.quoted_Price, product.quoted_Price);
            hashtable.Add(Em_Product.quoted_PriceBegin, product.quoted_PriceBegin);
            hashtable.Add(Em_Product.quoted_PriceEnd, product.quoted_PriceEnd);
        }
        private void ToPrice(string indication)
        {
            decimal price = 0;
            if (indication.IndexOf("万") > 0)
            {
                indication = indication.Replace("万", "");
                if (decimal.TryParse(indication, out price))
                {
                    product.pro_Price = price * 10000;
                }
            }
            else
            {
                if (decimal.TryParse(indication, out price))
                {
                    product.pro_Price = price;
                }
            }
        }
        private void ToQuoPrice(string quotedPrice)
        {
            quotedPrice = quotedPrice.Replace("￥", "");
            if (quotedPrice.IndexOf("至") > 0)
            {
                string[] array = quotedPrice.Split('至');
                decimal price1 = 0;
                decimal price2 = 0;
                if (decimal.TryParse(array[0].Trim(), out price1))
                {
                    product.quoted_PriceBegin = price1;
                }
                if (decimal.TryParse(array[1].Trim(), out price2))
                {
                    product.quoted_PriceEnd = price2;
                }
            }
        }
        /// <summary>
        /// 获取列表页面
        /// </summary>
        private void CheckData4()
        {
            IList<Product> productList = new List<Product>();
            string prolist = FormParam("prolist", "");
            prolist = HttpUtility.HtmlDecode(prolist);
            #region
            StreamWriter SW;
            SW = File.CreateText(Server.MapPath("~/33.txt"));
            SW.WriteLine(prolist);
            SW.Close();
            string path = Server.MapPath("~/33.txt");
            StringBuilder sb = new StringBuilder();
            using (StreamReader sr = new StreamReader(path, Encoding.UTF8))// Encoding.GetEncoding("GBK")
            {
                while (!sr.EndOfStream) //读到结尾退出
                {
                    string temp = sr.ReadLine();
                    sb.Append(temp);
                }
            }
            prolist = sb.ToString();
            product = new Product();
            product.main_param = prolist;
            #endregion
            int subcateId = FormParamInt32("subcateId", 0);
            int manuId = FormParamInt32("manuId", 0);
            //string reg = @"<input name=""proId[]"" [\s\S]*?value=""(?<value>.*?)"" [\s\S]*?>";

            string reg = "<div class=\"item\">\\s*<label [\\s\\S]*?><input [\\s\\S]*?value=\"(?<value>.*?)\" [\\s\\S]*?></label>\\s*" +
                "<div class=\"pic\">\\s*<a [\\s\\S]*?><img src=\"(?<src>.*?)\" [\\s\\S]*?></a>[\\s\\S]*?" +
                "<a [\\s\\S]*? class=\"title\" [\\s\\S]*?>(?<name>.*?)</a>[\\s\\S]*?"+
                "<span class=\"price\">￥<b >(?<price>.*?)</b>[\\s\\S]*?"+
                "<span class=\"date\">(?<date>.*?)</span>[\\s\\S]*?"+
                "<ul class=\"param\">(?<param>.*?)</ul>\\s*</div>";
            Regex r = new Regex(reg, RegexOptions.None);
            Match match = r.Match(prolist);
            while (match.Success)
            {
                string value = match.Groups["value"].ToString();
                int id = 0;
                if (Int32.TryParse(value, out id))
                {
                    Product pro = new Product();
                    pro.subcateId = subcateId;
                    pro.manuId = manuId;
                    pro.ProId = id;
                    pro.pro_Pic = match.Groups["src"].ToString();
                    pro.ProName = match.Groups["name"].ToString();
                    pro.quoted_Price = match.Groups["date"].ToString();
                    decimal price = 0;
                    pro.price_Indication = match.Groups["price"].ToString();
                    if (decimal.TryParse(pro.price_Indication, out price))
                    {
                        pro.pro_Price = price;
                    }
                    
                    string param = match.Groups["param"].ToString();
                    param = Regex.Replace(param, "<a [\\s\\S]*?>更多参数>></a>", "<a href=\"ProductInfo\" target=\"_blank\">更多参数>></a>");
                    //param = Regex.Replace(param, "<a href=\"/\\d+/\\d+/review.shtml\">[\\s\\S]*?</a>", "");
                    //param = Regex.Replace(param, "<a href=\"/\\d+/\\d+/video.shtml\" target=_blank>[\\s\\S]*?</a>", "");
                    //param = Regex.Replace(param, "<a href=\"http:\\/\\/oabbs.zol.com.cn/comment_\\d+.html\" target=_blank>[\\s\\S]*?</a>", "");
                    pro.main_param = param;
                    //pro.SerializeXmlFile("d:\\122.xml");
                    productList.Add(pro);
                }
                
                match = match.NextMatch();  
            }
            //productList.SerializeXmlFile("d:\\122.xml");
            foreach (var item in productList)
            {
                product = ProBll.GetById(item.ProId);
                if (product == null)
                {
                    ProBll.Insert(item);
                }
                else
                {
                    hashtable.Clear();
                    hashtable.Add(Em_Product.subcateId, item.subcateId);
                    hashtable.Add(Em_Product.manuId, item.manuId);
                    hashtable.Add(Em_Product.pro_Pic, item.pro_Pic);
                    hashtable.Add(Em_Product.ProName, item.ProName);
                    hashtable.Add(Em_Product.quoted_Price, item.quoted_Price);
                    hashtable.Add(Em_Product.price_Indication, item.price_Indication);
                    hashtable.Add(Em_Product.pro_Price, item.pro_Price);
                    hashtable.Add(Em_Product.main_param, item.main_param);
                    hashtable.Add(Em_Product.ProId, item.ProId);
                    ProBll.UpDate(hashtable);
                }
            }
            
        }

    }
}